// _Exercise4_BackgroundSubstraction4_Gaussian.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


// int _tmain(int argc, _TCHAR* argv[])
// {
int main(int argc, char** argv)
{
	/*
	CvCapture* capture = 0;

	if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
		capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
	else if( argc == 2 )
		capture = cvCaptureFromAVI( argv[1] );
		*/
	CvCapture* capture = cvCreateFileCapture("D:\\Leavy4a_dataset.avi");

	if( !capture )
	{
		printf("Error in open file avi...\n");
//		printf(stderr,"Could not initialize...\n");
		return -1;
	}

	/* print a welcome message, and the OpenCV version */
	printf ("Demo of the background classification using CvGaussBGModel %s (%d.%d.%d)\n",
		CV_VERSION,
		CV_MAJOR_VERSION, CV_MINOR_VERSION, CV_SUBMINOR_VERSION);

	/* Capture 1 video frame for initialization */
	IplImage* videoFrame = NULL;
	videoFrame = cvQueryFrame(capture);

	if(!videoFrame)
	{
		printf("Bad frame \n");
		exit(0);
	}

	// Create windows
	cvNamedWindow("BG", 1);
	cvNamedWindow("FG", 1);

	// Select parameters for Gaussian model.
	CvGaussBGStatModelParams* params = new CvGaussBGStatModelParams;						
	params->win_size=2;	
	params->n_gauss=5;
	params->bg_threshold=0.7;
	params->std_threshold=3.5;
	params->minArea=15;
	params->weight_init=0.05;
	params->variance_init=30; 

	// Creat CvBGStatModel
	// cvCreateGaussianBGModel( IplImage* first_frame, CvGaussBGStatModelParams* parameters )
	// or
	// cvCreateGaussianBGModel( IplImage* first_frame )
	
//	CvBGStatModel* bgModel = cvCreateGaussianBGModel(videoFrame ,params);
	CvBGStatModel* bgModel = cvCreateGaussianBGModel(videoFrame);

	int key=-1;
	while(key != 'q')
	{
		// Grab a fram
		videoFrame = cvQueryFrame(capture);
		if( !videoFrame )
			break;

		// Update model
		cvUpdateBGStatModel(videoFrame,bgModel);

		// Display results
		cvShowImage("BG", bgModel->background);
		cvShowImage("FG", bgModel->foreground);	
		key = cvWaitKey(10);
		if(key == 27)
			break;
	}

	cvDestroyWindow("BG");
	cvDestroyWindow("FG");
	cvReleaseBGStatModel( &bgModel );
	cvReleaseCapture(&capture);
	return 0;

	
}

